package com.crm.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.Currency;
import com.crm.util.ApplicationContext;

public class CurrencyDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	
	public  int insert(Currency cu)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
			session.saveOrUpdate(cu);
			tx.commit();
			return 0;
		}
		catch (Exception ex) {
		     if (tx!=null) tx.rollback();
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public  Currency findById(int i) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Currency c=(Currency)session.load(Currency.class, i);
			return c;
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public  int delete(Currency s) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
			session.delete(s);
			tx.commit();
			return 0;
		}
		catch (Exception ex) {
		     if (tx!=null) tx.rollback();
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public  Currency getCurrncy(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String querystr="from Currency where  business_id=:busid ";
			Query query=session.createQuery(querystr);
			
			query.setInteger("busid",busid);
			Object queryresult=query.uniqueResult();
			Currency c=(Currency)queryresult;
			return c;
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
	public  Currency getCurrncyById(int busid,int id) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String querystr="from Currency where  business_id=:busid and id=:id";
			Query query=session.createQuery(querystr);
			
			query.setInteger("busid",busid);
			query.setInteger("id",id);
			Object queryresult=query.uniqueResult();
			Currency c=(Currency)queryresult;
			return c;
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}
}
